<!--评论模块-->
<template>
  <div class="container">

    
        <div class="write-reply"  @click="newEvaluation()">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showComment">
            <el-input class="gray-bg-input"
                      v-model="params.text"
                      type="textarea"
                      :rows="3"
                      autofocus
                      placeholder="写下你的评论">
            </el-input>
             <el-rate
              v-model="params.score"
              :colors="['#99A9BF', '#F7BA2A', '#FF9900']">
            </el-rate>
            匿名评论：
            <el-switch
              v-model="params.evaluationType"
              active-color="#13ce66"
              inactive-color="#909399"
              active-value="0"
              inactive-value="1">
            </el-switch>
            <div class="btn-control">
              <span class="cancel" @click="closeNewEvaluation">取消</span>
              <el-button class="btn" type="success" round @click="commitPerson">确定</el-button>
            </div>
          </div>
        </transition>
    <div class="comment" v-for="item in commentData">

      <div class="info">
        <img class="avatar" :src="item.avatar" width="36" height="36" @click="userIndex(item)"/>
        <div class="right">
          <div class="name">{{item.userName}}</div>
          <div class="date">{{item.createTime}}</div>
        </div>
      </div>
      <div class="content">{{item.text}}</div>
      <div v-if="typeof  item.personId == 'undefined'">
        <el-rate
        v-model="item.score"
        disabled
        show-score
        text-color="#ff9900"
        score-template="{value}"
        :colors="['#99A9BF', '#F7BA2A', '#FF9900']">
      </el-rate>
      </div>
      <div class="control"  @mouseenter="deleteButtonShow = item.evaluationId" @mouseleave ="deleteButtonShow = '' ">
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span>回复</span>
        </span>
        <span class="comment-reply">
          <i class="iconfont icon-comment"></i>
        
              <span  v-show="deleteButtonShow===item.evaluationId&&$store.state.user.userType===0" @click="deleteEvaluation1(item)" >删除</span>
          
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="childrenReply in item.childrenReply">
          <div class="reply-content">
            <span class="from-name">{{childrenReply.userName}}</span><span>: </span>
            <span class="to-name">@{{childrenReply.personName}}</span>
            <span>{{childrenReply.text}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{childrenReply.createTime}}</span>
            <span class="reply-text" @click="showChildrenCommentInput(item, childrenReply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.evaluationId">
            <el-input class="gray-bg-input"
                      v-model="params.text"
                      type="textarea"
                      :rows="3"
                      autofocus
                      placeholder="写下你的评论">
            </el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
      <el-button  v-if="!noMore" type="text" @click="load()">加载更多</el-button>
     <p v-if="loading">加载中...</p>
    <p v-if="noMore">没有更多了</p>
    <el-dialog
      title="是否删除评论"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>子评论也会随之删除</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteEvaluation2()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

  import Vue from 'vue'
import {addPersonEvaluation,addSonEvaluation,addLikeNum,cancelLikeNum,deleteEvaluation} from '@/api/evaluation'
import { getEvaluationList } from '@/api/evaluation'
  export default {
    props: {
      internshipId:{
        type: Number,
        required: true
      }
    },
    components: {},
    data() {
      return {
        commentData:[],
        inputComment: '',
        showItemId: '',//当前要回复的评论ID
        showItemUserName:'',
        params:{
          text:'',
          score:5,
          internshipId:this.internshipId,
          evaluationType:'1',
          personAccount:'',
          personName:''
        },
        param:{
          companyId:this.internshipId,
          start:0,
          limit:10
        },
        showComment:false,
        iconClasses: ['icon-rate-face-1', 'icon-rate-face-2', 'icon-rate-face-3'],
        likeCommit:{
          evaluationId:''
        },
        loading: false,
        total:0,
        deleteButtonShow:true,
        deleteParams:{
          evaluationId:''
        },
        dialogVisible: false
      }
    },
    computed: {
      noMore () {
        return this.param.start+this.param.limit >= this.total
      },
      disabled () {
        return this.loading || this.noMore
      }
    },
    created() {
      this.getList();
    },
    methods: {
      userIndex(item){
        if(item.evaluationType===1){//实名评价
             this.$router.push({
              path: "/userIndex",
              query:{
                userAccount:item.userAccount,
                userType:item.userType
              }
              })
        }else{//匿名评价
        this.$message('这是一条匿名评论哦！');
        }
      },
      async getList(){
        this.param.start = 0;
        this.param.limit = 10;
        console.log(this.param)
        let res = await getEvaluationList(this.param);
        this.commentData = res.beans;
        this.total = res.bean.total;
      },

      async load () {
        this.loading = true
        this.param.start = this.param.start+this.param.limit;
        this.param.limit = 2;
        let res = await getEvaluationList(this.param);
        res.beans.filter( (item) =>{
        this.commentData.push(item)
        } )
        this.loading = false
      },
      /**
       * 点赞
       */
      likeClick(item) {
        this.likeCommit.evaluationId = item.evaluationId;
        if (item.isLike === null) {
          Vue.$set(item, "isLike", true);
          item.likeNum++
          //调用接口增加点赞数
          addLikeNum(this.likeCommit).then(()=>{
            this.$message({
              type: 'success',
              message: '点赞成功!'
            })
            }
          ).catch(err=>{
            this.$message({
              type: 'success',
              message: '点赞失败!'
            })
            })
        } else {
          if (item.isLike) {
            item.likeNum--
            //取消点赞
             cancelLikeNum(this.likeCommit).then(()=>{
            this.$message({
              type: 'success',
              message: '取消点赞成功!'
            })
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '取消点赞失败!'
            })
            })
          } else {
            item.likeNum++
            //调用接口增加点赞数
            addLikeNum(this.likeCommit).then(()=>{
            this.$message({
              type: 'success',
              message: '点赞成功!'
            })
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '点赞失败!'
            })
            })
          }
          item.isLike = !item.isLike;
        }
      },

      /**
       * 点击取消按钮
       */
      cancel() {
        this.showItemId = ''
      },

      /**
       * 提交父评论
       */
      commitPerson() {
        this.params.personId =''
        this.params.personName=''
        addPersonEvaluation(this.params).then(()=>{
            this.$message({
              type: 'success',
              message: '评论成功!'
            })
             this.params.text ='';
             this.getList();
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '评论失败!'
            })
            })
           
      },
      //删除评论
      deleteEvaluation1(item) {
        this.deleteParams.evaluationId = item.evaluationId
        this.dialogVisible = true
      },
      deleteEvaluation2(){
           deleteEvaluation(this.deleteParams).then(()=>{
            this.$message({
              type: 'success',
              message: '删除评论成功!'
            })
             this.getList();
             this.dialogVisible = false
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '删除评论失败!'
            })
            })
      },
       /**
       * 提交子级评论
       */
      commitComment() {
        this.params.personId = this.showItemId;
        addSonEvaluation(this.params).then(()=>{
            this.$message({
              type: 'success',
              message: '评论成功!'
            })
             this.params.text ='';
             this.getList();
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '评论失败!'
            })
            })
           
      },

      /**
       * 点击回复按钮显示评论输入框
       * item: 当前的评论
       * childrenReply: 当前回复的评论
       */
      showCommentInput(item) {
        this.showComment = false
        this.inputComment = ''
        this.showItemId = item.evaluationId;
        this.showItemUserName = item.userAccount;
        this.params.personAccount = item.userAccount;
        this.params.personName = item.userName;
      },
      
      /**
       * 点击子评论回复按钮显示评论输入框
       * item: 当前的评论
       * childrenReply: 当前回复子评论
       */
      showChildrenCommentInput(item, childrenReply) {
        console.log(childrenReply)
        if (childrenReply) {
          this.inputComment = "@" + childrenReply.userName + " "
        } else {
          this.inputComment = ''
        }
        this.showItemId = item.evaluationId;
        this.showItemUserName = item.userAccount;
        this.params.personName = childrenReply.userName;
      },
      
      /**
       * 点击添加新评论评论按钮显示输入框
       */
      newEvaluation() {
        console.log("添加新评论");
        this.showComment = !this.showComment;
        this.showItemId = '';
        this.params.personId = '';

      },
      closeNewEvaluation() {
        this.showComment = false;
      }

    }
  }
</script>

<style scoped lang="scss">

  .container {
    padding: 0 10px;
    box-sizing: border-box;
    .write-reply {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #909399;
          padding: 10px;
          cursor: pointer;
          &:hover {
            color: #303133;
          }
          .el-icon-edit {
            margin-right: 5px;
          }
        }
      .input-wrapper {
          padding: 10px;
          .gray-bg-input, .el-input__inner {
            /*background-color: #67C23A;*/
          }
          .btn-control {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding-top: 10px;
            .cancel {
              font-size: 16px;
              color: #606266;
              margin-right: 20px;
              cursor: pointer;
              &:hover {
                color: #333;
              }
            }
            .confirm {
              font-size: 16px;
            }
          }
        }
    .comment {
      display: flex;
      flex-direction: column;
      padding: 10px;
      border-bottom: 1px solid #F2F6FC;
      
      .info {
        display: flex;
        align-items: center;
        .avatar {
          border-radius: 50%;
        }
        .right {
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          .name {
            font-size: 16px;
            color: #303133;
            margin-bottom: 5px;
            font-weight: 500;
          }
          .date {
            font-size: 12px;
            color: #909399;
          }
        }
      }
      .content {
        font-size: 16px;
        color: #303133;
        line-height: 20px;
        padding: 10px 0;
      }
      .control {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: #909399;
        .like {
          display: flex;
          align-items: center;
          margin-right: 20px;
          cursor: pointer;
          &.active, &:hover {
            color: #409EFF;
          }
          .iconfont {
            font-size: 14px;
            margin-right: 5px;
          }
        }
        .comment-reply {
          display: flex;
          align-items: center;
          cursor: pointer;
          &:hover {
            color: #333;
          }
          .iconfont {
            font-size: 16px;
            margin-right: 5px;
          }
        }

      }
      .reply {
        margin: 10px 0;
        border-left: 2px solid #DCDFE6;
        .item {
          margin: 0 10px;
          padding: 10px 0;
          border-bottom: 1px dashed #EBEEF5;
          .reply-content {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: #303133;
            .from-name {
              color: #409EFF;
            }
            .to-name {
              color: #409EFF;
              margin-left: 5px;
              margin-right: 5px;
            }
          }
          .reply-bottom {
            display: flex;
            align-items: center;
            margin-top: 6px;
            font-size: 12px;
            color: #909399;
            .reply-text {
              display: flex;
              align-items: center;
              margin-left: 10px;
              cursor: pointer;
              &:hover {
                color: #333;
              }
              .icon-comment {
                margin-right: 5px;
              }
            }
          }
        }
        .write-reply {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: #909399;
          padding: 10px;
          cursor: pointer;
          &:hover {
            color: #303133;
          }
          .el-icon-edit {
            margin-right: 5px;
          }
        }
        .fade-enter-active, fade-leave-active {
          transition: opacity 0.5s;
        }
        .fade-enter, .fade-leave-to {
          opacity: 0;
        }
        .input-wrapper {
          padding: 10px;
          .gray-bg-input, .el-input__inner {
            /*background-color: #67C23A;*/
          }
          .btn-control {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding-top: 10px;
            .cancel {
              font-size: 16px;
              color: #606266;
              margin-right: 20px;
              cursor: pointer;
              &:hover {
                color: #333;
              }
            }
            .confirm {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
  </style>